//
//  MCSweepPayViewController.swift
//  StoreManage
//
//  Created by MC on 2018/5/17.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCSweepPayViewController: MCViewController {

    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        textField.text = ""
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc func beginSweepButtonClicked() {
        
        

        
        var moneyStr = textField.text ?? "0"
        
        if moneyStr.isEmpty {
            moneyStr = "0"
        }
        
        let money = Double(moneyStr)! * 100
        
        if money < 1 {
            textField.text = "0.01"
            MCHUD_text(message: "收款金额必须大于等于0.01元", view: self.view, delay: 1.2)
            return
        }
        
        let vc = MCScanViewController()
        vc.payMoney = money
        vc.scanType = .sweepPayScan
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)
    }
    
    @objc func qrPayViewEvent() {
        let vc = MCQrCodeCollectionViewController()
        MCPushToViewController(current: self, target: vc, isHiddenBottomBarWhenBack: true)

    }
    
    // MARK: - Network
    func sendNetworking() {
        
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "收款"
        self.view.backgroundColor = UIColor.MCGray_light
    }
    
    func initUI() {
        view.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.left.top.equalTo(view).offset(10)
            make.right.equalTo(-10)
            make.height.equalTo(280)
        }
        
        bgView.addSubview(storeNameLabel)
        storeNameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.top.right.equalTo(0)
            make.height.equalTo(80)
        }
        
        bgView.addSubview(textField)
        textField.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.right.equalTo(-45)
            make.top.equalTo(storeNameLabel.snp.bottom).offset(60)
            make.height.equalTo(40)
        }
        
        bgView.addSubview(lineView)
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.bottom.equalTo(textField)
            make.right.equalTo(view).offset(-15)
            make.height.equalTo(1)
        }
        
        bgView.addSubview(beginSweepButton)
        beginSweepButton.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(bgView)
            make.size.equalTo(CGSize.init(width: MCScreenWidth - 80, height: 40))
            make.top.equalTo(lineView.snp.bottom).offset(36)
        }
        
        view.addSubview(QRPayView)
        QRPayView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(54)
            make.top.equalTo(bgView.snp.bottom).offset(10)
        }
    }
    
    // MARK: - Setter & Getter
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()
    
    lazy var storeNameLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(20)
        label.text = "    " + MCUserinfo_getStoreName()
        label.textColor = UIColor.MCWords
        label.backgroundColor = MCColor_16(colorStr: "f8f8f8")
        return label
    }()
    
    lazy var textField: MCTextFiled = {
        let tf = MCTextFiled()
        tf.textAlignment = NSTextAlignment.center
        tf.keyboardType = UIKeyboardType.decimalPad
        tf.delegate = self
        tf.placeholder = "请输入金额"
        tf.font = MCFont(20)
        
        let label = UILabel()
        label.font = MCFont(30)
        label.text = "¥"
        label.textColor = UIColor.MCWords
        label.textAlignment = NSTextAlignment.right
        label.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
        tf.leftViewMode = UITextFieldViewMode.always
        tf.leftView = label
        return tf
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_middle
        return view
    }()

    
    lazy var beginSweepButton: UIButton = {
        let button = UIButton.init(type: .custom)
        button.titleLabel?.font = MCFont(14)
        button.backgroundColor = UIColor.MCMain
        button.setTitle("扫码收款", for: .normal)
        button.layer.cornerRadius = 5
        button.layer.masksToBounds = true
        button.setTitleColor(UIColor.white, for: .normal)
        button.addTarget(self, action: #selector(beginSweepButtonClicked), for: .touchUpInside)
        return button
    }()
    
    lazy var QRPayView: MCReuse_entryView = {
        let view = MCReuse_entryView()
        view.titleLabel.text = "  二维码收款"
        view.backgroundColor = UIColor.white
        view.titleLabel.font = MCFont(15)
        view.iconImageView.image = #imageLiteral(resourceName: "SweepPay_wallet")
        view.isShowArrow = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(qrPayViewEvent))
        view.addGestureRecognizer(tap)
        return view
    }()
}

extension MCSweepPayViewController : UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        if self.textField == textField {
            let b = MCString_keepTwoDecimalPlaces(textField, range, string)
            return b
        }
        return true
    }

}
